﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Assist;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YArchitech.HVAC.PipeSetting
{
	public partial class FrmPipeMaterial : HYBaseForm
	{
		public FrmPipeMaterial()
		{
			this.isInit = true;
			this.InitializeComponent();
		}

		public FrmPipeMaterial(string configFilePath) : this()
		{
			if (!File.Exists(configFilePath))
			{
				throw new Exception("盈建科管材配置文件找不到！");
			}
			this.configFilePath = configFilePath;
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			string text = this.cbbPipeMaterial.Text;
			if (string.IsNullOrEmpty(text))
			{
				return;
			}
			PipeMaterial pipeMaterial = new PipeMaterial();
			pipeMaterial.MaterialName = text;
			if (this.PipeMaterialData.ContainsKey(text))
			{
				this.PipeMaterialData[text].Add(pipeMaterial);
			}
			else
			{
				IList<PipeMaterial> list = new List<PipeMaterial>();
				list.Add(pipeMaterial);
				this.PipeMaterialData.Add(text, list);
			}
			this.RefreshList(this.PipeMaterialData[text]);
			this.dgvList.Rows[this.dgvList.RowCount - 1].Selected = true;
		}

		private void btnDel_Click(object sender, EventArgs e)
		{
			if (this.dgvList.SelectedRows.Count <= 0)
			{
				YJKMessageBox.Show("请选择要删除的行!");
				return;
			}
			string text = this.cbbPipeMaterial.Text;
			if (string.IsNullOrEmpty(text))
			{
				return;
			}
			int index = this.dgvList.SelectedRows[0].Index;
			object value = this.dgvList.SelectedRows[0].Cells[0].Value;
			if (value == null)
			{
				return;
			}
			PipeMaterial item = value as PipeMaterial;
			this.PipeMaterialData[text].Remove(item);
			this.RefreshList(this.PipeMaterialData[text]);
			int num = index - 1;
			if (num >= 0)
			{
				this.dgvList.Rows[num].Selected = true;
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.OK;
			try
			{
				this.SaveUserData();
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				YJKMessageBox.Show("保存数据失败!");
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void cbbPipeMaterial_TextChanged(object sender, EventArgs e)
		{
			string text = this.cbbPipeMaterial.Text;
			foreach (PipeMaterial pipeMaterial in this.PipeMaterialData[text])
			{
				this.dgvList.Rows.Add(new object[]
				{
					pipeMaterial,
					pipeMaterial.Diameter,
					pipeMaterial.InDiameter,
					pipeMaterial.OutDiameter
				});
			}
		}

		private void cbbPipeMaterial_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void FrmPipeMaterial_Load(object sender, EventArgs e)
		{
			this.dgvList.AllowUserToAddRows = false;
			this.PipeMaterialData = this.GetPipeMaterialData();
			this.cbbPipeMaterial.DataSource = this.PipeMaterialData.Keys.ToList<string>();
			this.isInit = false;
		}

		private void dgvList_CellValueChanged(object sender, DataGridViewCellEventArgs e)
		{
			if (this.isInit)
			{
				return;
			}
			int columnIndex = e.ColumnIndex;
			int rowIndex = e.RowIndex;
			object value = this.dgvList.Rows[rowIndex].Cells[columnIndex].Value;
			if (value == null)
			{
				YJKMessageBox.Show("单元格值不能为空!");
				return;
			}
			double num = 0.0;
			if (!double.TryParse(value.ToString(), out num))
			{
				YJKMessageBox.Show("请输入正确的格式！");
				return;
			}
			if (num == 0.0)
			{
				YJKMessageBox.Show("数值不能是0！");
				return;
			}
			string headerText = this.dgvList.Columns[columnIndex].HeaderText;
			object value2 = this.dgvList.Rows[rowIndex].Cells[0].Value;
			if (value2 == null)
			{
				YJKMessageBox.Show("数据有误！");
				return;
			}
			PipeMaterial pipeMaterial = value2 as PipeMaterial;
			if (headerText == "公称直径")
			{
				pipeMaterial.Diameter = num;
				return;
			}
			if (headerText == "内径")
			{
				pipeMaterial.InDiameter = num;
				return;
			}
			if (!(headerText == "外径"))
			{
				return;
			}
			pipeMaterial.OutDiameter = num;
		}

		private void RefreshList(IList<PipeMaterial> taskList)
		{
			this.dgvList.Rows.Clear();
			if (taskList == null)
			{
				return;
			}
			foreach (PipeMaterial pipeMaterial in taskList)
			{
				this.dgvList.Rows.Add(new object[]
				{
					pipeMaterial,
					pipeMaterial.Diameter,
					pipeMaterial.InDiameter,
					pipeMaterial.OutDiameter
				});
			}
		}

		private IDictionary<string, IList<PipeMaterial>> GetPipeMaterialData()
		{
			Dictionary<string, IList<PipeMaterial>> dictionary = new Dictionary<string, IList<PipeMaterial>>();
			PipeMaterial pipeMaterial = new PipeMaterial();
			pipeMaterial.MaterialName = "ppr";
			pipeMaterial.Diameter = 125.0;
			pipeMaterial.InDiameter = 100.0;
			pipeMaterial.OutDiameter = 130.0;
			((IDictionary<string, IList<PipeMaterial>>)dictionary).Add("ppr", new List<PipeMaterial>
			{
				pipeMaterial
			});
			return dictionary;
		}

		private void SaveUserData()
		{
		}

		private void dgvList_SelectionChanged(object sender, EventArgs e)
		{
			if (this.dgvList.SelectedRows.Count <= 0)
			{
				return;
			}
			this.dgvList.SelectedRows[0].Selected = true;
		}

		private bool isInit;

		private readonly string configFilePath = Path.Combine(Product.UserDataLocation, "YJKPipingSystemTypeSetInformation.xml");

		private IDictionary<string, IList<PipeMaterial>> PipeMaterialData = new Dictionary<string, IList<PipeMaterial>>();
	}
}
